package com.siteseat.common.json;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.deser.StdDeserializer;

import com.siteseat.common.exception.InternalException;
import com.siteseat.common.misc.TimeHelper;

public class DateDeserializer extends StdDeserializer<Date> {

  /**
   * 
   */
  public DateDeserializer() {
    super( Date.class );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Date deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException, JsonProcessingException {
    try {
      return TimeHelper.parse( jp.getText() );
    } catch( ParseException e ) {
      throw new InternalException( "Invalid date format", e );
    }
  }

}
